package klassen;

import java.util.Random;

public class MyTask implements Runnable {

	/**
	 * Alternativ:
	 * 
	 * extends Thread
	 */

	private boolean alive;

	@Override
	public void run() {

		alive = true;

		long start = System.currentTimeMillis();

		int bestMove = Integer.MIN_VALUE;
		Random ranom = new Random();
		for (int i = 0; i <= 100; i++) {
			for (int j = 0; j < 8000; j++) {
				for (int k = 0; k < 8000; k++) {
					synchronized (this) {
						if (!alive) {
							System.out.println("Stopping!");
							return;
						}
					}

					int move = ranom.nextInt();
					if (move > bestMove) {
						bestMove = move;
					}
				}

			}
			System.out.println("Thinking... " + i + "%");
		}

		long stop = System.currentTimeMillis();
		long time = stop - start;
		System.out.println(Thread.currentThread().getName() + " Fertig nach "
				+ time / 1000 + "sekunden");

	}

	/**
	 * 
	 */
	public void cancel() {
		synchronized (this) {
			alive = false;
		}
	}
}
